Beginning WebGL for HTML5 by Brian Danchilla

Beginning WebGL for HTML5 by Brian Danchilla

Author:Brian Danchilla
Language: eng
Format: epub, pdf
ISBN: 9781430239963
Publisher: Apress


Figure 6-4. Mandelbrot set (left); some regions of self-similarity highlighted (right)

In Listing 6-4, we set constants of the number of iterations to test for each point being in the set and the amount to multiply the color of the boundary points based on the iteration number when a number leaves the set. When setting the c-value, the x coordinate has been moved 0.5 in the negative direction just to center the image better. The first z-value, z1, equals c,so we set z initially equal to c. We initialize the pixel color to black and start our loop. The next z-value is calculated from the equations that we have derived in Listing 6-3 and then we test the dot product for being over 4. Recall that the dot product is x2+ y2, which is the magnitude (or length) squared. We know that a magnitude larger than 2 means that the point is outside the set. As 22 = 4, using the squared value is an equivalent but less-expensive calculation because it does not require us to compute a square root. Finally we shade our pixel a value of blue between 0 and 1 based on the current iteration when the pixel becomes unbound in the sequence to the maximum number of iterations. However, it can be hard to see the boundary, so setting the constant LIGHTNESS_FACTOR to a higher value such as 10.0 will help visualize the boundary. Doing so increases the number of white-colored iterations, but this does provide much better contrast.

Note If the fragment shader is not loading externally, try placing it inline. I have experienced errors with the for loop when placed in an external file, but have the program working when inline. The actual reason for this is that the comparision sign “<” needs to be escaped in the shader if it is read in as XML. A couple ways to resolve this issue are shown in the debugging section of Chapter 9.The for loop in GLSL is much more restrictive than in JavaScript or C/C++ and requires a constant as the limit condition.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.